﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize] 
    public class BuyStockController : Controller
    {
       BuyStock_BLL _BuyStock_BLL = new BuyStock_BLL();
       Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();
       Supplier_BLL _Supplier_BLL = new Supplier_BLL();
       ProductCatalog_BLL _ProductCatalog_BLL = new ProductCatalog_BLL();
        private M_DueFund_BLL _M_DueFund_BLL = new M_DueFund_BLL();
          public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _BuyStock_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
             model = model.Where(a => a.ProductName.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }
        

        public virtual ActionResult Details(Guid Id)
        {
            ViewBag.Supplier = _Supplier_BLL.Get();
            ViewBag.ProductCatalog = _ProductCatalog_BLL.Get();
            var item = _BuyStock_BLL.Get(Id);
            return View(item);
        }
        
        public virtual ActionResult Edit(Guid? Id)
        {
            if (_Supplier_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加采购管理中的供应商管理')</script>");
                Response.End();
            }
            if (_ProductCatalog_BLL.Get().Count() == 0)
            {
                Response.Write("<script>alert('请先添加销售管理中的产品分类')</script>");
                Response.End();
            }
            ViewData["Supplier"] = _Supplier_BLL.Get();
            ViewData["ProductCatalog"] = _ProductCatalog_BLL.Get(); 
            var item = _BuyStock_BLL.Get(Id);

           
            return View(item);
        }

        [HttpPost]
        [ValidateInput(false)]
         public virtual ActionResult Edit(Guid? Id, BuyStock collection)
        {
            if (!ModelState.IsValid)
            {
                if (Request["ProductName"] == null)
                {
                    ModelState.AddModelError("ProductName", "产品名称不能为空");
                }
                if (!ModelState.IsValid)
                {
                    Edit(Id);
                    return View(collection);


                }
            }
           
                collection.TotalPrice = collection.CostPrice * int.Parse(collection.Unit);
           
            
           _BuyStock_BLL.Save(Id, ref collection);
            return RedirectToAction("Index",MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
           // var item = _BuyStock_BLL.Get(Id);
            //foreach (var item1 in item.M_DueFund)
            //{
            //    _M_DueFund_BLL.Delete(item1.Id);
            //} 
            _BuyStock_BLL.Delete(Id);
            return RedirectToAction("Index",MyrouteValues.Get());
        }
    }
}
